import { defineStore } from 'pinia'
import { getCurrentUser, searchByTag } from '../../api/user/userApi'
const useUserStore = defineStore('user', {
  state: () => {
    return {
      editUser: {
        editName: '',
        keyWord: '',
        params: '',
      },
      userInfo:{

      },
      tagList:[],
      isFlash:false,
      userId:null,
      avartarUrl:''
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    setEditUser(editName, keyWord, params) {
      console.log('params', params)

      this.editUser = { editName, keyWord, params }
    },
    updateAvartar(params){
      this.avartarUrl=params
    },
    getCurrentUserInfo() {
        return new Promise((resolve,reject)=>{
            getCurrentUser().then((res) => {
                console.log(res)
                try {
                  resolve(res.data)
                } catch (error) {
                  reject(error)
                }
                this.userInfo=res.data
                
              })
        })
      
    },
    getSearchByTag(tagList){
      return new Promise(async (resolve,reject)=>{
        const res=await searchByTag(tagList)
        if(res.code===0){
          resolve(res.data)
        }else{
          reject()
        }
      })
    },
    set_tagList(tagList){
      this.tagList=tagList
    },
    judgeFlash(val){
      this.$state.isFlash=val
    },
    //设置当前登录用户id
    set_userId(userId){
      this.userId=userId
    }
  },
  getters: {
    IS_FLASH:(state)=>{
      return state.isFlash
    },
    TAGLIST:(state)=>{
      return state.tagList
    },
    AVARTAR:(state)=>{
      return new Promise((resolve)=>{
        return resolve(state.avartarUrl)
      })
    }

  },
})
export default useUserStore
